using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.ConstrainedExecution;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UI_ItemSlot : MonoBehaviour,IPointerDownHandler,IPointerEnterHandler, IPointerExitHandler
{
    [SerializeField] protected Image itemImage;
    [SerializeField] protected TextMeshProUGUI itemAmountText;
    protected InventoryItem item;



    public virtual void UpdateSlot(InventoryItem item)
    {
        if (item != null)
        {
            this.item = item;
            itemImage.sprite = item.data.icon;
            itemImage.color = Color.white;
            if (item.sizeStack > 1)
            {
              
                itemAmountText.text = item.sizeStack.ToString();
            }
            else
            {
                itemAmountText.text = "";
            }
        }
        else
        {
            this.item = null;
            itemImage.color = new Color(1, 1, 1, 0);
        }
    }
    public virtual void OnPointerDown(PointerEventData eventData)
    {
        if (item != null)
        {
            ItemContorlManager.instance.ShowItemControl(item.data, GetComponent<RectTransform>().position);
            if (item.data.Type == ItemType.Equipment)
            {
                ItemContorlManager.instance.EquipItem = () => InventoryManager.instance.Equip(item);
            } else if (item.data.Type == ItemType.Prop)
            {
                ItemContorlManager.instance.UseItem = () => InventoryManager.instance.UseItem(item);
                ItemContorlManager.instance.EquipItem = () => InventoryManager.instance.EquipFlask(item);
            }
        }
    }

    public virtual void OnPointerEnter(PointerEventData eventData)
    {

        ItemInfoShowManager.instance.ShowItemInfo(item, eventData.position);
    }

    public virtual void OnPointerExit(PointerEventData eventData)
    {
        ItemInfoShowManager.instance.UnShowInfo();
    }
}
